﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using RestSharp;
using WebApiDemo.Contract;
using Ymatou.ServiceCenter;

namespace WebApiDemo.Controllers
{
    [RoutePrefix("api/Default")]
    public class DefaultController : ApiController
    {
        // GET: api/Default
        public IEnumerable<string> Get()
        {
            string clientIp = Request.GetClientIp();
            string serverIpLink = Request.GetServerIpLink();
            return new string[] { "value1", "value2" };
        }

        // GET: api/Default/5
        public string Get(int id)
        {
            return "value:" + id;
        }

        // POST: api/Default
        public string Post([FromBody]string value)
        {
            return "POST: api/Default";
        }

        // Put: api/Default
        public string Put()
        {
            string orderId = Request.Headers.GetValues("OrderId").FirstOrDefault();
            //return "put";
            return orderId;
        }

        // PUT: api/Default/5
        public string Put(int id, [FromBody]string value)
        {
            return string.Format("Id:{0},Value:{1}", id, value);
        }

        // DELETE: api/Default/5
        public void Delete(int id)
        {
        }

        //[ServiceRegister("api/Default/TestPostJson",Method.POST)]
        [Route("TestPostJson")]
        [HttpPost]
        public TestPostJsonResponse TestPostJson(TestPostJsonRequest request)
        {
            if (request == null)
                return null;
            TestPostJsonResponse response = new TestPostJsonResponse
            {
                Id = request.Id,
                Data = string.Format("Name:{0},Age:{1},Price:{2}", request.Name, request.Age, request.Price)
            };
            return response;
        }

        //[ServiceRegister("api/Default/TestPost", Method.POST)]
        [Route("TestPost")]
        [HttpPost]
        //public TestPostResponse TestPost([FromBody]TestPostRequest request)
        //[ServiceRegister("api/Default/TestPost?id={id}&name={name}&age={age}&price={price}", Method.POST)]
        public TestPostResponse TestPost(string id, string name, int age, decimal price)//parameterBuildType.UrlQueryString
        {
            //MediaTypeHeaderValue contentType = this.Request.Content.Headers.ContentType;
            TestPostRequest request = new TestPostRequest
            {
                Id = id,
                Name = name,
                Age = age,
                Price = price
            };
            TestPostResponse response = new TestPostResponse
            {
                Id = request.Id,
                Data = string.Format("Name:{0},Age:{1},Price:{2}", request.Name, request.Age, request.Price)
            };
            return response;
        }

        [Route("TestPostResult")]
        [HttpPost]
        //public Result<TestPostResponse> TestPostResult([FromBody] TestPostRequest request)
        public Result<TestPostResponse> TestPostResult(TestPostRequest request)
        {
            //假定我们业务中定义了 StatusCode=1表示成功，StatusCode = -1 表示失败
            try
            {
                TestPostResponse response = new TestPostResponse
                {
                    Id = request.Id,
                    Data = string.Format("Name:{0},Age:{1},Price:{2}", request.Name, request.Age, request.Price)
                };

                //throw new Exception("error");  //假设在这儿抛出异常但是又没有对其catch后重新包装返回Result,则客户端方法调用的结果将是方法参数中指定的一个Result

                return new Result<TestPostResponse>() { Response = response, RequestId = Request.GetRequestId(), StatusCode = 1 };
            }
            catch (Exception ex)
            {
                return new Result<TestPostResponse>() { RequestId = Request.GetRequestId(), Message = ex.Message, StatusCode = -1 };
            }
        }

        //[ServiceRegister("api/Default/TestGet", Method.GET)]
        [Route("TestGet")]
        [HttpGet]
        //public TestGetResponse TestGet([FromUri]TestGetRequest request)

        public TestGetResponse TestGet(string id, string name, int age, decimal price)
        {//FormUrlEncodedContent
            //MediaTypeHeaderValue contentType = this.Request.Content.Headers.ContentType;
            TestGetRequest request = new TestGetRequest
            {
                Id = id,
                Name = name,
                Age = age,
                Price = price
            };
            TestGetResponse response = new TestGetResponse
            {
                Id = request.Id,
                Data = string.Format("Name:{0},Age:{1},Price:{2}", request.Name, request.Age, request.Price)
            };

            return response;
        }

        //[ServiceRegister("api/Default/TestPut", Method.PUT)]
        //[ServiceRegister("api/Default/TestPut?id={id}&name={name}&age={age}&price={price}", Method.PUT)]
        [Route("TestPut")]
        [HttpPut]
        public TestPutResponse TestPut(string id, string name, int age, decimal price)
        //public TestPutResponse TestPut([FromBody]TestPutRequest request)
        //public TestPutResponse TestPut(TestPutRequest request)
        {
            //MediaTypeHeaderValue contentType = this.Request.Content.Headers.ContentType;
            TestPutRequest request = new TestPutRequest
            {
                Id = id,
                Name = name,
                Age = age,
                Price = price
            };
            TestPutResponse response = new TestPutResponse
            {
                Id = request.Id,
                Data = string.Format("Name:{0},Age:{1},Price:{2}", request.Name, request.Age, request.Price)
            };
            return response;
        }

        //[ServiceRegister("api/Default/TestDelete", Method.DELETE)]
        [Route("TestDelete")]
        [HttpDelete]
        public TestDeleteResponse TestDelete(TestDeleteRequest request)
        {
            TestDeleteResponse response = new TestDeleteResponse
            {
                Id = request.Id,
                Data = string.Format("Name:{0},Age:{1},Price:{2}", request.Name, request.Age, request.Price)
            };
            return response;
        }
    }
}
